import Vue from 'vue';
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate';

Vue.use(Vuex);

export default new Vuex.Store({
  state: Vue.observable( {
    count: 1,
  }),
  mutations: {
    save (state, payloads) {
      for (let k in payloads) {
        state[k] = payloads[k];
      }
    }
  },
  actions: {
    updateCount ({commit}, {value = 1, delay = 3000} = {}) {
      return new Promise(resolve => {
        setTimeout(() => {
          commit('save', {count: value});
          resolve();
        }, delay);
      });
    },
    updateStateB ({commit}, {b}) {
      commit('save', {b});
    }
  },
  modules: {

  },
  plugins: [
    createPersistedState({
      filter: ({type}) => type.search(new RegExp('^[loading|route]')) > 0,
    }),
  ]
});

